/*!
 * 
 * Copyright(c) 2016 huangbinglong
 * MIT Licensed
 */

'use strict';

angular.module("app", ["dayu.ui.grid", 'ui.grid', 'ui.grid.pagination','ui.grid.selection', 'ui.grid.resizeColumns','ui.grid.exporter'])
    .controller('MainCtrl', function ($scope, DayuGrid,uiGridConstants,$http,$timeout) {
        $scope.dayuGridOptions = DayuGrid({
            getPage : function(paginationOptions,callback) {
                var self = this;
                var url;
                switch(paginationOptions.sort) {
                    case uiGridConstants.ASC:
                        url = '/example/dayuGrid/data/100_ASC.json';
                        break;
                    case uiGridConstants.DESC:
                        url = '/example/dayuGrid/data/100_DESC.json';
                        break;
                    default:
                        url = '/example/dayuGrid/data/100.json';
                        break;
                }

                $http.get(url)
                    .success(function (data) {
                        var firstRow = (paginationOptions.pageNumber - 1) * paginationOptions.pageSize;
                        var subData = data.slice(firstRow, firstRow + paginationOptions.pageSize);
                        callback(100,subData);
                    });
            },
            gridOptions: {
                columnDefs: [
                    {field: 'name', displayName: "姓名", width: 100},
                    {field: 'gender', displayName: "性别", width: 100, enableSorting: false},
                    {field: 'company', displayName: "公司", enableSorting: false}
                ]
            },
            action: {
                view: {
                    label: "查看",
                    type: "view",
                    show:"name=='Ethel Price'",
                    listens: [
                        function ($location, dayuGrid) {
                            dayuGrid.navigation().go();
                        }
                    ]
                }
            }
        }, $scope);
        $scope.dayuGridOptions  = $scope.dayuGridOptions.resetScope($scope);
        $scope.gridOptions = $scope.dayuGridOptions.gridOptions();
        $scope.dayuGridOptions.onRegisterApi(function(gridApi) {
           console.log("onRegisterApi:"+gridApi);
        });
        //$timeout(function() {
        //    $scope.dayuGridOptions.updateColumnDefs([
        //        {field: 'name', displayName: "姓名", width: 100}
        //    ]);
        //},3000);
    }
);
